package name.jautzy.esi.client;

import java.util.ArrayList;
import java.util.List;

import name.jautzy.esi.client.ressources.StandardRessources;

import name.jautzy.esi.client.model.*;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BaseTreeLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.data.TreeLoader;
import com.google.gwt.user.client.rpc.AsyncCallback; 
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToggleButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.LabelField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;

public class PreferencesListener implements Listener<ButtonEvent> {
private ContentPanel parent = null;
private ContentPanel mypanel = null;
	
	public PreferencesListener (ContentPanel parent) {
		this.parent = parent;
	}
	
	public void handleEvent(ButtonEvent ce) {  
        if (!ce.<ToggleButton> getComponent().isPressed()) {  
          return;  
        }  
        
        AsyncCallback<EsiUser> callback = new AsyncCallback<EsiUser>() {
        	public void onSuccess(EsiUser user) {
        		
        		mypanel = new FormPanel();
                
                FormData formData = new FormData(200,20);
                
                final String id = user.getId();
                
                LabelField mail = new LabelField(user.getId());  
                mypanel.add(mail, formData);  
                
                final TextField<String> firstName = new TextField<String>();  
                firstName.setFieldLabel("Character");  
                firstName.setAllowBlank(false);  
                firstName.setValue(user.getNick());
                mypanel.add(firstName, formData);  
                
                final TextField<String> corpo = new TextField<String>();  
                corpo.setFieldLabel("Corporation");  
                corpo.setAllowBlank(true);  
                corpo.setValue(user.getCorpo());
                mypanel.add(corpo, formData);  
                
                final TextField<String> alliance = new TextField<String>();  
                alliance.setFieldLabel("Alliance");  
                alliance.setAllowBlank(true);  
                alliance.setValue(user.getAlliance());
                mypanel.add(alliance, formData);  
                
                Button save = new Button("Save");
                save.addSelectionListener(new SelectionListener<ButtonEvent>() {  
    		    	@Override  
    				public void componentSelected(ButtonEvent ce) {
    		    		
    		    		AsyncCallback<Void> callback2 = new AsyncCallback<Void>() {
    		    			
    						public void onSuccess(Void result) {
    							Esi.getEsi().addMessage("Preferences saved");
    						}

    						public void onFailure(Throwable caught) {
    							Esi.getEsi().addMessage("can't save Preferences : "+caught.toString());
    						}
    					};
    					EsiUser newUser = new EsiUser();
    					newUser.setId(id);
    					newUser.setAlliance(alliance.getValue());
    					newUser.setCorpo(corpo.getValue());
    					newUser.setNick(firstName.getValue());
    					Esi.getEsi().getLoginService().updateUser(newUser, callback2);
    					
    				}  
    			}); 
                mypanel.addButton(save);
                
        	    parent.removeAll();  
        	    parent.add(mypanel);  
        	    parent.layout();     
        		
        	}
        	public void onFailure(Throwable caught) {
				MessageBox.info("Action", "Can't load user : "+caught.toString(), null); 
			}
        };
        
        Esi.getEsi().getLoginService().getCurrentUser(callback);
	}  
}
